//
//  SelectBtnView.m
//  ProjectForCustomer
//
//  Created by apple on 2019/8/28.
//  Copyright © 2019年 钟游. All rights reserved.
//

#import "SelectBtnView.h"

@implementation SelectBtnView
{
    NSMutableArray * selectArray;
    UIView * firstView;
    UIView * secondView;
}
-(instancetype)initWithFrame:(CGRect)frame withDataArray:(NSArray*)dataArray;{
    if (self=[super initWithFrame:frame]) {
        selectArray = [NSMutableArray new];
        [self createUIWithDataArray:dataArray];
    }
    return self;
}

#pragma mark -- btnClick
-(void)btnClick:(CustomButton*)sender{
    
    sender.selected = !sender.selected;
    NSInteger tag = sender.tag - 100;
    AddInfoModel * model = [self.baseDataArray objectAtIndex:tag];
    sender.backgroundColor = sender.selected?UIColorFromRGB(0x14C4FB):[UIColor whiteColor];
    if (sender.selected) {
        if (selectArray.count!=3) {
             [selectArray addObject:model];
        }
    }else{
        [selectArray removeObject:model];
    }
    
    [self insertUI];
}
-(void)editBtnClick:(CustomButton*)sender{
    NSInteger tag = sender.tag-1000;
    AddInfoModel * model = [selectArray objectAtIndex:tag];
    [self.baseDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        AddInfoModel * models = obj;
        if ([model.tagId isEqualToString:models.tagId]) {
            CustomButton * btn = [self viewWithTag:100 + idx];
            btn.selected = NO;
            btn.backgroundColor=[UIColor whiteColor];
            *stop = YES;
        }
    }];
    [selectArray removeObjectAtIndex:tag];
    [self insertUI];
    
}
-(void)setBaseDataArray:(NSMutableArray *)baseDataArray{
    _baseDataArray=baseDataArray;
    [selectArray removeAllObjects];
    [self setNeedsLayout];
}
-(void)layoutSubviews{
    for (id object in firstView.subviews) {
        [object removeFromSuperview];
    }
    for (id object in secondView.subviews) {
        [object removeFromSuperview];
    }
    [self loadSecondViewWithDataArray:self.baseDataArray];
}

#pragma mark -- insert
-(void)insertUI{
    NSArray * array = firstView.subviews;
    for (id object in array) {
        [object removeFromSuperview];
    }
      CGFloat width = (KscreenWidth-50)/3;
    NSString *  favouriteDirector = @"";
    for (int i = 0; i<selectArray.count; i++) {
        AddInfoModel * model =[selectArray objectAtIndex:i];
        CustomButton * btn =[[CustomButton alloc]initWithFrame:CGRectMake(15+i*(width+10), 0, width, 30) andTitle:model.tagName andColor:UIColorFromRGB(0x333333) andFont:14];
        CustomLabel * lab =[[CustomLabel alloc]initWithFrame:CGRectMake(width-5-5, 5, 5, 5) andText:@"x" andFont:5 andColor:[UIColor blackColor]];
        [btn addSubview:lab];
        btn.tag= 1000 + i;
        [btn addTarget: self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [firstView addSubview:btn];
         favouriteDirector =[favouriteDirector stringByAppendingString:model.tagId];
        if (i!=selectArray.count-1) {
                favouriteDirector =[favouriteDirector stringByAppendingString:@","];
        }
    }
    if (self.selectBlock) {
        self.selectBlock(favouriteDirector);
    }
    
    
}
#pragma mark --second view
-(void)loadSecondViewWithDataArray:(NSArray*)array{
    CGFloat width = (KscreenWidth-50)/3;
    NSInteger x = 0;
    NSInteger y = 0;
    for (int i = 0; i<array.count; i++) {
        if (x==3) {
            x=0;
        }
        y = i/3;
        AddInfoModel * model = array[i];
        CustomButton * btn =[[CustomButton alloc]initWithFrame:CGRectMake(15+x*(width+10), 50*y, width, 30) andTitle:model.tagName andColor:UIColorFromRGB(0x333333) andFont:14];
        btn.layer.cornerRadius = 2;
        btn.layer.borderWidth=.5;
        btn.layer.borderColor =  UIColorFromRGB(0xDEDEDE).CGColor;
        btn.tag = 100 +i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [secondView addSubview:btn];
        x++;
    }
}
#pragma mark -- UI
-(void)createUIWithDataArray:(NSArray*)dataArray{
    CustomLabel * titleLabel =[[CustomLabel alloc]initWithFrame:CGRectMake(15, 10, KscreenWidth-30, 20) andText:@"已选标签" andFont:13 andColor:[UIColor blackColor]];
    [self addSubview:titleLabel];
    
    firstView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame), KscreenWidth, 60)];
    [self addSubview:firstView];
    
    secondView=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(firstView.frame)+20, KscreenWidth, 220)];
    [self addSubview:secondView];
    [self loadSecondViewWithDataArray:self.baseDataArray];
}
@end
